<?php
require_once (dirname(__FILE__) . "/session_auth.php");
// we don't have to update $_SESSION
session_write_close();
?>
<?php

require_once (dirname(__FILE__) . "/config.php");
require_once (dirname(__FILE__) . "/response.php");
require_once (dirname(__FILE__) . "/class/widgetPool/WidgetPool.php");
require_once (dirname(__FILE__) . "/debug.php");
?>
<?php

/*
 * Widget Package Manager
 * Created: 200910 (Roger Wang)
 */
// load data from POST string
$widgetRequest = json_decode($HTTP_RAW_POST_DATA, true);
if($widgetRequest == null){
    // input format error
    make_error_response(WPM_ERRCODE_INVALIDFORMAT, WPM_ERRCODE_INVALIDFORMAT_MSG);
    exit();
}
// check act
if(! isset($widgetRequest["act"])){
    // we can do nothing without 'act'
    make_error_response(WPM_ERRCODE_NOACT, WPM_ERRCODE_NOACT_MSG);
    exit();
}
// userid
if(! isset($_SESSION['uid'])){
    make_error_response(WPM_ERRCODE_WFERR, WPM_ERRCODE_WFERR_MSG . ": retrieving userid failed");
    exit();
}
$userid = $_SESSION['uid'];
switch($widgetRequest['act']){
    case "check":
        /*$widgetpoolobj = new WFWidgetPool();
        if($widgetpoolobj->isFailed()){
            make_error_response(WPM_ERRCODE_INITERR, WPM_ERRCODE_INITERR_MSG);
            exit();
        }
        if(! $widgetpoolobj->check_pool()){
            $resultarray['Status'] = - 1;
            make_successful_response(WPM_NO_NEED_UPDATE, $resultarray);
        }else{
            $resultarray['Status'] = 0;
            make_successful_response(WPM_NEED_UPDATE, $resultarray);
        }*/
        try{
            // $strUpdateType = widget, configure_widget_and_widget_component
            $strUpdateType = isset($widgetRequest['update_type']) ? $widgetRequest['update_type'] : 'widget';
            $strFuncName = 'is'.WF::getTypeFactory()->getString()->getUpperCamelCase($strUpdateType).'Update';
            $isUpdate = WF::getWidgetPoolFactory()->getWidgetPoolManager($strUpdateType)->$strFuncName();
            if(false === $isUpdate){
                $resultarray['Status'] = - 1;
                make_successful_response(WPM_NO_NEED_UPDATE, $resultarray);
            }else{
                $resultarray['Status'] = 0;
                make_successful_response(WPM_NEED_UPDATE, $resultarray);
            }
        }catch(Exception $e){
            $response_code = $e->getCode();
            make_error_response($response_code, 'Update Failed! Err Msg :' . $e->getMessage());
        }
        break;
    case "check_new": //check if got new widget in db or not
        $isNewWidget = WF::getWidgetPoolFactory()->getWidgetPoolManager()->isNewWidget();
        //echo $isNewWidget;
        if(false === $isNewWidget){
            // below message is not show in UI, so do not need to L10N
            make_error_response('There is no new widget in DB!', (string) intval($isNewWidget));
        }else{
            // below message is not show in UI, so do not need to L10N
            make_successful_response('There is got new widget in DB!', (string) intval($isNewWidget));
        }
        break;
    case "check_new_scm":
        if(file_exists($GLOBALS['scm_path'])){
            make_successful_response('folder exist', '0');
        }
        else{
            make_error_response('folder does not exist', '1');
        }
        break;
    case "update":
        $response_code = '0'; //success
        try{
            // $strUpdateType = widget, configure_widget, widget_component
            $strUpdateType = isset($widgetRequest['update_type']) ? $widgetRequest['update_type'] : 'widget';
            // If widget_component update, you must as assign type $widgetRequest['product_id']
            $mixProductId = isset($widgetRequest['product_id']) && $widgetRequest['product_id'] ? $widgetRequest['product_id'] : '';
            WF::getWidgetPoolFactory()->getWidgetPoolManager($strUpdateType)->updateWidget($mixProductId);
            if('0' === $response_code){
                // below message is not show in UI, so do not need to L10N
                make_successful_response('Update Success!', $response_code);
            }
        }catch(Exception $e){
            $response_code = $e->getCode();
            make_error_response($response_code, 'Update Failed! Err Msg :' . $e->getMessage());
        }
        break;
    case "get_configure_widget_and_wiget_component":
        try{
            // $strUpdateType = widget, configure_widget, widget_component
            $strUpdateType = 'widget_component';
            $assocsUpdateStatusForConfigureWidget = WF::getWidgetPoolFactory()->getWidgetPoolManager($strUpdateType)->getAssocsUpdateStatusForConfigureWidgetAndWidgetComponent($widgetRequest['isNewWcuUpdate']);
            // below message is not show in UI, so do not need to L10N
            make_successful_response(null, $assocsUpdateStatusForConfigureWidget);
        }catch(Exception $e){
            $response_code = $e->getCode();
            make_error_response($response_code, 'Get configure widget and wiget component for wcu update failed! Err Msg :' . $e->getMessage());
        }
        break;
    default:
        make_error_response(WPM_ERRCODE_CMDERR, WPM_ERRCODE_CMDERR_MSG);
        break;
}
